// CustomCell.m
// By Jayson Adams, NeXT Developer Support Team
// You may freely copy, distribute and reuse the code in this example.
// NeXT disclaims any warranty of any kind, expressed or implied, as to its
// fitness for any particular use.

// This subclass of cell draws a dot image to the right of the text
// and provides fancy, printable highlighting.

#import <appkit/appkit.h>
#import "CustomCell.h"

@implementation CustomCell

  
#define IMAGEMARGIN 4.0
  
- drawInside:(const NXRect *)cellFrame inView:controlView
{
    /* every CustomCell needs these two */
    static id dotImage = nil;
    static id sharedTextCell = nil;

    NXRect rect = *cellFrame;
    NXPoint imageOrigin;
    NXSize dotSize;
        
    if (!dotImage) {
	dotImage = [NXImage findImageNamed:"Dot"];
    }
    [dotImage getSize:&dotSize];

  /* erase the cell */
    PSsetgray((cFlags1.state || cFlags1.highlighted) ? NX_WHITE : NX_LTGRAY);
    NXRectFill(cellFrame);

  /* draw the "attachment" image */
    imageOrigin.x = NX_X(cellFrame) + IMAGEMARGIN;
    imageOrigin.y = NX_Y(cellFrame) + NX_HEIGHT(cellFrame) -
    		    (NX_HEIGHT(cellFrame) - dotSize.height) / 2.0;

    [dotImage composite:NX_SOVER toPoint:&imageOrigin];
      
    NX_WIDTH(&rect) -= (dotSize.width + IMAGEMARGIN * 2.0 - NX_X(&rect));
    NX_X(&rect) += dotSize.width + IMAGEMARGIN * 2.0;

    if (!sharedTextCell) {
        sharedTextCell = [[Cell alloc] init];
	[sharedTextCell setWrap:NO];
    }
    [sharedTextCell setFont:[self font]];
    [sharedTextCell setStringValue:[self stringValue]];
    [sharedTextCell drawInside:&rect inView:controlView];
    
  /* all drawing from now on will be in dark gray */
    PSsetgray(NX_DKGRAY);
    
  /* draw the two dark gray lines above and below the cell */
    if (cFlags1.state || cFlags1.highlighted) {
        NXRect rectArray[2];
      /*
       * draw 1-pixel tall rectangles instead of lines (this is faster than
       * PSmoveto(); PSlineto()).
       */
	NXSetRect(&(rectArray[0]), NX_X(cellFrame), NX_Y(cellFrame),
		NX_WIDTH(cellFrame), 1.0);
	NXSetRect(&(rectArray[1]), NX_X(cellFrame), NX_MAXY(cellFrame) - 1.0,
		NX_WIDTH(cellFrame), 1.0);

      /* using NXRectFillList is faster than separate calls to NXRectFill */
	NXRectFillList(rectArray, 2);
    }

    return self;
}

- highlight:(const NXRect *)cellFrame inView:controlView lit:(BOOL)flag
{
    if (cFlags1.highlighted != flag) {
	cFlags1.highlighted = flag;
	[self drawInside:cellFrame inView:controlView];
    }
    return self;
}

@end